#include "Epix3D.h"
#include <iostream>

using namespace std;

void grey_seuil(unsigned char *map, int i, unsigned char n, int s)
{
	for(int j=0; j<=255;)
	{
		if(n >= j && n < j+255/s)
		{
			map[i] = (255/s)*j;
			map[i+1] = (255/s)*j;
			map[i+2] = (255/s)*j;
			map[i+3] = 255;
		}
		if(j == 255)
			j++;
		else
		{
			j+=255/s;
			if(j > 255)
				j = 255;
		}
	}
}

void color_seuil(unsigned char *map, int i, unsigned char n)
{
	if(n >= 0 && n < 60)
	{
		map[i] = 0;
		map[i+1] = 0;
		map[i+2] = 150;
	}
	else if(n >= 60 && n < 80)
	{
		map[i] = 200;
		map[i+1] = 200;
		map[i+2] = 0;
	}
	else if(n >= 80 && n < 130)
	{
		map[i] = 0;
		map[i+1] = 150;
		map[i+2] = 0;
	}
	else if(n >= 130 && n < 200)
	{
		map[i] = 200;
		map[i+1] = 100;
		map[i+2] = 0;
	}
	else if(n >= 200 && n < 255)
	{
		map[i] = 200;
		map[i+1] = 200;
		map[i+2] = 200;
	}
	map[i+3] = 255;
}

void seuil(unsigned char *map, int i, unsigned char n)
{
	//grey_seuil(map, i, n, 8);
	color_seuil(map, i, n);
	/*map[i] = n;
	map[i+1] = n;
	map[i+2] = n;
	map[i+3] = 255;*/
}

void lightning(float X, float Y, float Z)
{
	//glEnable(GL_LIGHTING);
	//glEnable(GL_LIGHT0);
	//glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION , 0.01);
	// ambient scene
	GLfloat lmodel_ambient[] = {0.0,0.0,0.0,1.0};
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);
	// ambient light
	GLfloat amb[] = {1.0,1.0,1.0,1.0};
	glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
	// diffuse
	GLfloat dif[] = {1.0,1.0,1.0,1.0};
	glLightfv(GL_LIGHT0,GL_DIFFUSE,dif);
	// specular
	GLfloat specular[] = {1.0,1.0,1.0,1.0};
	glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
	// position
	GLfloat l_pos[] = {X, Y, Z, 1.0};
	glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
	// MATERIAUX SIMPLES
	GLfloat ambient_m[] = { 0.0,0.0,0.0,1.0 };
	GLfloat diffuse_m[] = { 1.0,1.0,1.0,1.0 };
	GLfloat specular_m[] = { 0.0,0.0,0.0,1.0 };
	GLfloat shinines_m[] = { 50.0 };
	glMaterialfv(GL_FRONT,GL_AMBIENT, ambient_m);
	glMaterialfv(GL_FRONT,GL_DIFFUSE, diffuse_m);
	glMaterialfv(GL_FRONT,GL_SPECULAR,specular_m);
	glMaterialfv(GL_FRONT,GL_SHININESS,shinines_m); 
	//glDepthFunc(GL_LESS); 
	glEnable(GL_DEPTH_TEST);
	// dessine la lumière
	glPushMatrix();
	glColor3ub(255, 255, 0);
	GLUquadric* params = gluNewQuadric();
    gluQuadricDrawStyle(params,GLU_LINE);
    glTranslated(X, Y, Z);
    gluSphere(params,5.0,32,32);
    gluDeleteQuadric(params);
    glColor3ub(255, 255, 255);
    glPopMatrix();
}
